//
//  HTTPManager.m
//  Castlery
//
//  Created by xie weizhi on 8/8/17.
//  Copyright © 2017 xie weizhi. All rights reserved.
//

#import "HTTPManager.h"
#import "APIConfig.h"

@implementation HTTPManager

+ (instancetype)sharedManager {
    static HTTPManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] initWithBaseURL:[NSURL URLWithString:CastlerySeverIP]];
    });
    
    return sharedManager;
}

- (instancetype)initWithBaseURL:(NSURL *)url {
    if (![super initWithBaseURL:url]) {
        return nil;
    }
    
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    self.requestSerializer.timeoutInterval = NetworkRequestTimeOut;
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    
    return self;
}

- (void)requestWithPath:(NSString *)path
                 params:(NSDictionary *)params
                 method:(HTTPMethod)method
                success:(APIClientSuccessBlock)success
                failure:(APIClientFailureBlock)failure {
    switch (method) {
        case GET:
        {
            [self GET:path parameters:params progress:nil success:success failure:failure];
        }
            break;
        case POST:
        {
            [self POST:path parameters:params constructingBodyWithBlock:nil progress:nil success:success failure:failure];
        }
        case PUT:
        {
            [self PUT:path parameters:params success:success failure:failure];
        }
            break;
        default:
            break;
    }
}


@end
